
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author alrz
 */
public class Ventana extends JFrame implements MouseListener, MouseMotionListener{

   private ArrayList<Figura> figuras;
   private int size;
   
   
   
   public Ventana()
   {
       super("Ventana");
       super.setSize(800,600);
       super.setLayout(null);
       super.setLocationRelativeTo(null);
       super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       super.addMouseListener(this);
       super.addMouseMotionListener(this);
       this.size = 2;
         this.figuras = new ArrayList<Figura>();
       this.crearFiguras();
     
       
   }
   
   public void crearFiguras()
   {
        Random s = new Random();
        Figura matrizSol[][] = new Figura[size][size];
        
        for(int i = 0 ; i< size; i++)
        {
            for(int j = 0 ; j < size  ; j++)
            {
                Figura figuraTemp = new Figura(s.nextInt(10),s.nextInt(10),s.nextInt(10),s.nextInt(10));
                matrizSol[i][j] = figuraTemp;
                if(j>0)
                {
                    matrizSol[i][j].setLeft(matrizSol[i][j-1].getRight());
                }
                if(i>0)
                {
                    matrizSol[i][j].setUp(matrizSol[i-1][j].getDown());
                }
            }
        }
        
        int nAzar = s.nextInt(size)+1;
        int i = 0;
        int x1,y1,x2,y2;
        
        while(i<nAzar)
        {
            x1 = s.nextInt(size);
            y1 = s.nextInt(size);
            x2 = s.nextInt(size);
            y2 = s.nextInt(size);
            Figura temp = matrizSol[x1][y1];
            matrizSol[x1][y1] = matrizSol[x2][y2];
            matrizSol[x2][y2] = temp;  
            i++;
        }
        
        for(int x = 0; x<size; x++)
        {
            for(int y = 0; y<size; y++)
            {
                super.add(matrizSol[x][y]);
                matrizSol[x][y].setBounds((x+1)*100, (y+1)*100, 100,100);
                this.figuras.add(matrizSol[x][y]);
            }
        }
  
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Figura fig = null;
        
        if((fig=obtenerFiguraPresionada(e.getPoint())) != null)
        {
            fig.setPressed(true);
            this.remove(fig);
            this.add(fig);
            
            
        }
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
        Figura fig = null;
        if((fig = obtenerFigura())!= null)
        {
            fig.setPressed(false);
        }
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        
        Figura fig;
        if((fig = obtenerFigura())!= null)
        {
            fig.setBounds(e.getX(), e.getY(), 100, 100);
            this.repaint();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }

    private Figura obtenerFiguraPresionada(Point p)
    {
        for(Figura fig: this.figuras)
        {
            if(fig.getX()<=p.getX() && fig.getWidth()+fig.getX()>=p.getX() && fig.getY()<=p.getY() && fig.getHeight()+fig.getY() >= p.getY())
            {
                return fig;
            }
        }
        
        return null;
    }

    private Figura obtenerFigura()
    {
        for(Figura f : this.figuras)
        {
            if(f.isPressed() == true)
            {
                return f;
            }
        }
        return null;
    }
}